package com.zzb.service.impl;

import com.zzb.DTO.ProductDTO;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.ProductVO;
import com.zzb.dao.ProductDao;
import com.zzb.entity.ProductEntity;
import com.zzb.entity.UserEntity;
import com.zzb.service.ProductService;
import com.zzb.util.Tool;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    RedisTemplate<String, Object> redisTemplate;
    ProductEntity productEntity = new ProductEntity();
    @Resource
    ProductDao productDao;

    @Override
    public ResultDTO add(ProductVO productVO) {
        //数据复制
        BeanUtils.copyProperties(productVO, productEntity);
        if (productDao.add(productEntity) == 1) {
            return ResultDTO.getResult("success", 1);
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO delete(ProductVO productVO) {
        //数据复制
        BeanUtils.copyProperties(productVO, productEntity);
        //判断产品是否存在
        if (productDao.findById(productEntity) != null) {
            //存在
            return ResultDTO.getResult("success", productDao.delete(productEntity));
        } else {//不存在
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findById(ProductVO productVO) {
        BeanUtils.copyProperties(productVO, productEntity);
        productEntity = productDao.findById(productEntity);
        if (productEntity != null) {
            ProductDTO productDTO = new ProductDTO();
            BeanUtils.copyProperties(productEntity, productDTO);
            return ResultDTO.getResult("success", productDTO);
        } else {
            return ResultDTO.getResult("error", 0);
        }
    }

    @Override
    public ResultDTO findAll(ProductVO productVO) {
        BeanUtils.copyProperties(productVO, productEntity);
        int count = productDao.count(productEntity);
//        PageHelper.startPage(productEntity.getPage(),productEntity.getLimit());
        List<ProductEntity> list = productDao.findAll(productEntity);
        //集合复制
        List<ProductDTO> productDTOList = Tool.copy(list, ProductDTO.class);
        return ResultDTO.getResult(count, productDTOList);
    }

    @Override
    public ResultDTO update(ProductVO productVO) {
        BeanUtils.copyProperties(productVO, productEntity);
        return ResultDTO.getResult(productDao.update(productEntity));
    }

    @Override
    public void setProductToRedis() {
        List<ProductEntity> list = productDao.selectAll();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        for (ProductEntity entity : list) {
            valueOperations.set("product:" + entity.getId(), entity);
        }
    }
}
